import request from "@/api/index";
interface dataSourceType {
  [propName: string]: any;
}
export const checkCreatSiteForm = async (
  _rule: dataSourceType = {},
  value: any
) => {
  if(_rule.test == '1'){
    console.log(_rule, value, 'value')
  }
  if (_rule.required) {
    // 必填项验证非空并排除非必须项
    if (!value && value !== 0 && value !== false) {
      return Promise.reject(`${_rule.msg1}`);
    }
    if(value instanceof Array){
      if(value.length==0){
        return Promise.reject(`${_rule.msg1}`);
      }
    }
    if (
      _rule.key == "watermarkResourceId" ||
      _rule.key == "newContentResourceId"
    ) {
      console.log(_rule, "_rule_rule");
      return Promise.reject(`${_rule.msg1}`);
    }
  }
  if (_rule.value && _rule.value == value) {
    // 没变化没校验的必要
    return Promise.resolve();
  }
  if (_rule.api && value) {
    // 没填值没有校验的必要
    var params = detectKey(_rule.key, _rule, value);
    let res = await request[`${_rule.api}`](params);
    if (!res) {
      return Promise.reject(`${_rule.msg2}`);
    } else {
      return Promise.resolve();
    }
  }
};
export const detectKey = (
  key: string,
  obj: dataSourceType = {},
  value: any
) => {
  // 要检测的key:value
  let params: dataSourceType = {};
  params[key] = value;
  if (key == "siteDomain" || key == "siteDomainAlias") {
    params["siteProtocol"] = obj.params["siteProtocol"];
  }
  return params;
};
